package com.atguigu.eduservice;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.eduservice.service.impl.EduVideoServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@SpringBootTest
@RunWith(SpringRunner.class)
public class EduVideoController {

    private static final Logger logger = LoggerFactory.getLogger(EduVideoServiceImpl.class);

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;


    @Test
    public void Test01(){
        removeVideoByCourseId("18");
    }

    /**
     * 根据课程id删除小结
     * @param courseId
     */
    @Transactional(rollbackFor = Exception.class)
    public void removeVideoByCourseId(String courseId) {

        LambdaQueryWrapper<EduVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduVideo::getCourseId,courseId);
        queryWrapper.select(EduVideo::getVideoSourceId);//只查找一个字段提高效率
        List<EduVideo> list = videoService.list(queryWrapper);
        list.forEach(eduVideo ->  logger.info("遍历list:{}",eduVideo));
        List<String> ids = list.stream()
                .filter(item->StringUtils.hasText(item.getVideoSourceId()))
                .map(item ->{
                    boolean b = StringUtils.hasText(item.getVideoSourceId());
                    return item.getVideoSourceId();
                })
                .collect(Collectors.toList());
        logger.info("ids为：{}",ids);
        if (ids.size()>0){
            vodClient.deleteAlyVideos(ids);
        }

        //删除video表示的记录
        QueryWrapper<EduVideo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("course_id", courseId);
        videoService.remove(queryWrapper2);
    }
}
